package cn.codeforfun.follower;

import cn.codeforfun.vote.Option;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import me.chanjar.weixin.mp.bean.result.WxMpUser;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * Created by Administrator on 2017/2/16 0016.
 */
@Entity
@Table(name = "t_follower")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Follower {
    @Id
    @GeneratedValue
    private Long id;

    private Boolean subscribe;
    private String openId;
    private String nickname;
    private String sex;
    private String language;
    private String city;
    private String province;
    private String country;
    private String headImgUrl;
    private Long subscribeTime;
    private Integer sexId;
    private String remark;

    @OneToMany(mappedBy = "follower")
    @JsonBackReference
    public Set<FollowerOption> followerOptions = new LinkedHashSet<>();

    @Transient
    private String subscribeString;


    public Follower() {
    }

    public Follower(WxMpUser wxMpUser) {
        this.subscribe = wxMpUser.getSubscribe();
        this.openId = wxMpUser.getOpenId();
        this.nickname = wxMpUser.getNickname();
        this.sex = wxMpUser.getSex();
        this.language = wxMpUser.getLanguage();
        this.city = wxMpUser.getCity();
        this.province = wxMpUser.getProvince();
        this.country = wxMpUser.getCountry();
        this.headImgUrl = wxMpUser.getHeadImgUrl();
        this.subscribeTime = wxMpUser.getSubscribeTime();
        this.sexId = wxMpUser.getSexId();
        this.remark = wxMpUser.getRemark();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Boolean getSubscribe() {
        return subscribe;
    }

    public void setSubscribe(Boolean subscribe) {
        this.subscribe = subscribe;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getHeadImgUrl() {
        return headImgUrl;
    }

    public void setHeadImgUrl(String headImgUrl) {
        this.headImgUrl = headImgUrl;
    }

    public Long getSubscribeTime() {
        return subscribeTime;
    }

    public void setSubscribeTime(Long subscribeTime) {
        this.subscribeTime = subscribeTime;
    }

    public Integer getSexId() {
        return sexId;
    }

    public void setSexId(Integer sexId) {
        this.sexId = sexId;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }


    public String getSubscribeString() {
        if (this.subscribeTime == null) return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date(this.subscribeTime * 1000L));
    }

    public void setSubscribeString(String subscribeString) {
        this.subscribeString = subscribeString;
    }

    public Set<FollowerOption> getFollowerOptions() {
        return followerOptions;
    }

    public void setFollowerOptions(Set<FollowerOption> followerOptions) {
        this.followerOptions = followerOptions;
    }
}
